﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections.ObjectModel;

namespace TinyCinema.Data
{
    public class Ticket : Execution
    {
        public string ID { get; set; }
        public decimal Price { get; set; }
        public Schedule Schedule { get; set; }
        public Desk Desk { get; set; }
        public TicketOrder TicketOrder { get; set; }
        public BookedTicket BookedTicket { get; set; }
        public Ticket(Schedule schedule, Desk desk, decimal price)
        {
            ID = IDGenerate(Tables.Ticket);
            Schedule = schedule;
            Desk = desk;
            Price = price;
            Save();
        }
        public Ticket(string _ID)
        {
            ID = _ID;
            PutTableIntoDataSet(Tables.Ticket);
            var tbl = ds.Tables[Tables.Ticket];
            var Row = tbl.Select(TicketTable.ID + "='" + ID + "'")[0];
            ID = Row[TicketTable.ID] as string;
            Schedule = new Schedule(Row[TicketTable.ScheduleID] as string);
            Desk = new Desk(Row[TicketTable.DeskID] as string);
            Price = (decimal)Row[TicketTable.Price];
        }
        public void Save()
        {
            PutTableIntoDataSet(Tables.Ticket);
            var tbl = ds.Tables[Tables.Ticket];
            var Row = tbl.NewRow();
            Row[TicketTable.ID] = ID;
            Row[TicketTable.DeskID] = Desk.ID;
            Row[TicketTable.ScheduleID] = Schedule.ID;
            if (BookedTicket != null)
                Row[TicketTable.BookedTicketID] = BookedTicket.ID;
            else
                Row[TicketTable.BookedTicketID] = null;
            if (TicketOrder != null)
                Row[TicketTable.TicketOrder] = TicketOrder.ID;
            else Row[TicketTable.TicketOrder] = null;
            tbl.Rows.Add(Row);
            SaveCommand(Tables.Ticket);
        }
    }
}
